package com.febers.ihealth.fragment

import com.febers.ihealth.R
import com.febers.ihealth.base.BaseFragment
import com.febers.ihealth.entity.ReleaseNextSurveyEvent
import com.febers.ihealth.utils.Event
import com.febers.ihealth.utils.log
import com.febers.ihealth.utils.user
import kotlinx.android.synthetic.main.survey_weichangdao.*

class SurveyWeichangdao: BaseFragment() {

    private var checkedCount = 0
        set(value) {
            field = value
            if (field > 0) {
                Event.post(ReleaseNextSurveyEvent(true))
            } else {
                Event.post(ReleaseNextSurveyEvent(false))
            }
        }

    override fun setView(): Int = R.layout.survey_weichangdao

    override fun initView() {
        fun onCheckChange(isChecked: Boolean) {
            if (isChecked) {
                checkedCount++
            } else {
                checkedCount--
            }
        }
        radio_button_exin.setOnCheckedChangeListener { buttonView, isChecked ->
            user.exin = isChecked
            if (isChecked) {
                radio_button_wuzhengzhuang.clearCheck()
            }
            onCheckChange(isChecked)
        }
        radio_button_outu.setOnCheckedChangeListener { buttonView, isChecked ->
            user.outu = isChecked
            if (isChecked) {
                radio_button_wuzhengzhuang.clearCheck()
            }
            onCheckChange(isChecked)
        }
        radio_button_biantang.setOnCheckedChangeListener { buttonView, isChecked ->
            user.biantang = isChecked
            if (isChecked) {
                radio_button_fuxie.clearCheck()
                radio_button_shuiyangbian.clearCheck()
                radio_button_bianmi.clearCheck()
                radio_button_wuzhengzhuang.clearCheck()
            }
            onCheckChange(isChecked)
        }
        radio_button_fuxie.setOnCheckedChangeListener { buttonView, isChecked ->
            user.fuxie = isChecked
            if (isChecked) {
                radio_button_biantang.clearCheck()
                radio_button_bianmi.clearCheck()
                radio_button_wuzhengzhuang.clearCheck()
            }
            onCheckChange(isChecked)
        }
        radio_button_shuiyangbian.setOnCheckedChangeListener { buttonView, isChecked ->
            user.shuiyangbian = isChecked
            if (isChecked) {
                radio_button_biantang.clearCheck()
                radio_button_bianmi.clearCheck()
                radio_button_wuzhengzhuang.clearCheck()
            }
            onCheckChange(isChecked)
        }
        radio_button_fuzhang.setOnCheckedChangeListener { buttonView, isChecked ->
            user.fuzhang = isChecked
            if (isChecked) {
                radio_button_wuzhengzhuang.clearCheck()
            }
            onCheckChange(isChecked)
        }
        radio_button_shiyucha.setOnCheckedChangeListener { buttonView, isChecked ->
            user.shiyucha = isChecked
            if (isChecked) {
                radio_button_wushiyu.clearCheck()
                radio_button_wuzhengzhuang.clearCheck()
            }
            onCheckChange(isChecked)
        }
        radio_button_wushiyu.setOnCheckedChangeListener { buttonView, isChecked ->
            user.wushiyu = isChecked
            if (isChecked) {
                radio_button_shiyucha.clearCheck()
                radio_button_wuzhengzhuang.clearCheck()
            }
            onCheckChange(isChecked)
        }
        radio_button_bianmi.setOnCheckedChangeListener { buttonView, isChecked ->
            user.bianmi = isChecked
            if (isChecked) {
                radio_button_biantang.clearCheck()
                radio_button_fuxie.clearCheck()
                radio_button_shuiyangbian.clearCheck()
                radio_button_wuzhengzhuang.clearCheck()
            }
            onCheckChange(isChecked)
        }
        radio_button_wuzhengzhuang.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                radio_button_exin.clearCheck()
                radio_button_outu.clearCheck()
                radio_button_biantang.clearCheck()
                radio_button_fuxie.clearCheck()
                radio_button_shuiyangbian.clearCheck()
                radio_button_fuzhang.clearCheck()
                radio_button_shiyucha.clearCheck()
                radio_button_wushiyu.clearCheck()
                radio_button_bianmi.clearCheck()
            }
            onCheckChange(isChecked)
        }
    }

    override fun lazyInit() {
    }

    override fun onVisible() {
        Event.post(ReleaseNextSurveyEvent(false))
        if (checkedCount > 0) {
            Event.post(ReleaseNextSurveyEvent(true))
        }
    }
}